package com.zhb.zhbaiagent.tools;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;


import java.io.IOException;

/**
 * 使用Jsoup进行网页抓取
 */
public class WebScrapingTool {

    /**
     * 抓取指定URL的网页内容
     *
     * @param url 需要抓取的网页地址
     * @return 返回抓取到的网页HTML内容，如果抓取失败则返回错误信息
     */
    @Tool(description = "Scrape the content of a web page")
    public String scrapeWebPage(@ToolParam(description = "URL of the web page to scrape") String url) {
        try {
            Document doc = Jsoup.connect(url).get();
            return doc.html();
        } catch (IOException e) {
            return "Error scraping web page: " + e.getMessage();
        }
    }
}
